#!usr/bin/env python3
# -*- coding: utf-8 -*-

'define the main devices and equipment'

from os import pipe


_author_='Nan WANG'


class Module(object):


    # 定义膜元件面积，膜元件数量，初始通量，膜架材料，膜组器型号
    def __init__(self, area_e=30, qlt=60, flux=15, material='SS304', name='BDG-2030膜组器'):
        self.area_e = area_e
        self.qlt = qlt
        self.flux = flux
        self.material = material
        self.name = name
        self.area_m = area_e * qlt
# test
# print(Module().area_m)


class Pump(object):


    # 定义水泵流量，扬程，数量，备用，名称，变频器，材料
    def __init__(self, flow, head, qlt, qlt_back, name, vfd, material='铸铁'):
        self.flow = flow
        self.head = head
        self.qlt = qlt
        self.material = material
        self.qlt_back = qlt_back
        self.name = name
        self.vfd = vfd
        self.power_shaft = self.flow / 3.6 / 102 * self.head / 0.75
    # 计算电机功率

    def Power_motor(self):
        power_nominal = (0.55, 0.75, 1.1, 2.2, 3, 4, 5.5, 7.5,
                         11, 15, 18.5, 22, 30, 37, 45, 55, 75,
                         90, 110, 32, 160, 185, 200, 220)
        power_motor = [x for x in power_nominal if x >= self.power_shaft * 1.2]
        return power_motor[0]


class Pipe(object):


    # 定义管道流量，名称，类型，材料 # 气管 1表示主管，0 表示支管
    # 水管 position =1 表示泵前，-1 泵后，0 总管
    def __init__(self, flow, position=1, name='焊接钢管', material='Q235'):
        self.flow = flow
        self.name = name
        self.position = position
        self.material = material

        self.dia_nominal = [20, 25, 32, 40, 50, 65, 80, 100,
                            125, 150, 200, 250, 300, 350, 400,
                            450, 500, 550, 600, 650, 700, 750,
                            800, 850, 900, 950, 1000, 1200, 1500]
        self.dia_nominal_part1 = self.dia_nominal[:12]
        self.dia_nominal_part2 = self.dia_nominal[12:28]
        self.dia_nominal_part3 = self.dia_nominal[-2:]

    def velocity(self, v_try):
        velocity_try = self.flow / 3600 / 3.14 / v_try ** 2 * 4 * 1000000
        return velocity_try

    # 气管管径计算，支管和主管区分
    def dia_gas(self):
        dia_branch = []
        dia_main = []
        if self.position == 0:
            for v_try in self.dia_nominal:
                velocity_try = self.flow / 3600 / 3.14 / v_try ** 2 * 4 * 1000000
                if 4 <= velocity_try <= 5.2:
                    dia_branch.append(v_try)
            return dia_branch[0]

        elif self.position == 1:
            for v_try in self.dia_nominal:
                velocity_try = self.flow / 3600 / 3.14 / v_try ** 2 * 4 * 1000000
                if 10 <= velocity_try <= 15:
                    dia_main.append(v_try)
            return dia_main[0]

    # 通用水管管径计算
    def dia_water_normal(self):
        dia_normal = []
        for v_try in self.dia_nominal:
            velocity_try = self.flow / 3600 / 3.14 / v_try ** 2 * 4 * 1000000
            if 0.95 <= velocity_try <= 1.5:
                dia_normal.append(v_try)
        return dia_normal[0]

    # 泵前管管径计算
    def dia_water_pre(self):
        dia_pre = []
        for v_try in self.dia_nominal_part1:
            velocity_try = self.flow / 3600 / 3.14 / v_try ** 2 * 4 * 1000000
            if 1.0 <= velocity_try <= 1.2:
                dia_pre.append(v_try)
        for v_try in self.dia_nominal_part2:
            velocity_try = self.flow / 3600 / 3.14 / v_try ** 2 * 4 * 1000000
            if 1.2 <= velocity_try <= 1.6:
                dia_pre.append(v_try)
        for v_try in self.dia_nominal_part3:
            velocity_try = self.flow / 3600 / 3.14 / v_try ** 2 * 4 * 1000000
            if 1.5 <= velocity_try <= 2.0:
                dia_pre.append(v_try)
        return dia_pre[0]

    # 泵后管管径计算
    def dia_water_post(self):
        dia_post = []
        for v_try in self.dia_nominal_part1:
            velocity_try = self.flow / 3600 / 3.14 / v_try ** 2 * 4 * 1000000
            if 1.5 <= velocity_try <= 2.0:
                dia_post.append(v_try)
        for v_try in self.dia_nominal_part2:
            velocity_try = self.flow / 3600 / 3.14 / v_try ** 2 * 4 * 1000000
            if 2.0 <= velocity_try <= 2.5:
                dia_post.append(v_try)
        for v_try in self.dia_nominal_part3:
            velocity_try = self.flow / 3600 / 3.14 / v_try ** 2 * 4 * 1000000
            if 2.0 <= velocity_try <= 3.0:
                dia_post.append(v_try)
        return dia_post[0]


# test
# testpipe = Pipe(900)       
# print(testpipe.dia_gas())
# print(testpipe.dia_water_normal(), testpipe.dia_water_pre(), testpipe.dia_water_post())


class Gate_square(object):


    def __init__(self, qlt, flow, height=100, name='手电动进水方闸门'):
        self.qlt = qlt
        self.flow = flow
        self.name = name
        self.height = height
    
        self.height_try = [100 * h_try for h_try in range(1, 30)
         if 0.3 <= self.flow / 3600 / h_try ** 2 * 100 <= 1]

        self.height = self.height_try[-1]


class Gate_rect(object):


    def __init__(self, qlt, flow, length=800, height=800,
     H_weir=0.7, name='手电动进水方闸门', ):
        self.qlt = qlt
        self.flow = flow
        self.name = name
        self.height = height
        self.length = length
        self.H_weir = H_weir
        m = 0.405 + 0.0037 / self.H_weir
        lenght_try = self.flow / m / ( 2 * 9.81 ) ** 0.5 / self.H_weir ** 1.5
        self.length = int(lenght_try / 100) * 100
        self.velocity = self.flow / self.H_weir / self.length

# a = Gate_rect(4, 1700)
# print(a.name, a.length, a.velocity)


class Valve(Pipe):


    def __init__(self, qlt, flow, dia =100, position = 1, pressure=1.0, name='气动阀门'):
        self.qlt = qlt
        self.flow = flow
        self.name = name
        self.pressure = pressure
        self.position = position
        self.dia = dia
        self.dia_nominal = [20, 25, 32, 40, 50, 65, 80, 100,
                    125, 150, 200, 250, 300, 350, 400,
                    450, 500, 550, 600, 650, 700, 750,
                    800, 850, 900, 950, 1000, 1200, 1500]
        self.dia_nominal_part1 = self.dia_nominal[:12]
        self.dia_nominal_part2 = self.dia_nominal[12:28]
        self.dia_nominal_part3 = self.dia_nominal[-2:]


# aa = Valve(6, 300)
# print(aa.dia_gas())


class Blower(object):


# flow 单位为Nm3/h head为m
    def __init__(self, flow, head, name='磁悬浮鼓风机', temp=20):
        self.flow = flow / 60
        self.head = head
        self.name = name
        self.temp = temp
        self.power_shaft = (self.flow * 1.2 * 1.004 * (273.15 + temp) * 
        (((101 + head * 10) /101) ** 0.286 - 1) / 60 / 0.85)
        self.power_N = self.power_shaft / 0.97 / 0.97 * 1.1

    # 计算电机功率 返回鼓风机台数
    def Power_motor(self):
        power_nominal = (50, 75, 100, 200, 300, 400)
        if self.power_N <= 200:
            power_motor = [x for x in power_nominal if x >= self.power_N * 1.2]
            return power_motor[0], 1
        else:
            qlt = 2
            while self.power_N / qlt > 200:
                qlt = qlt + 1
                return 200, qlt
    

# bl = Blower(1000 * 60, 4.5)
# print(bl.Power_motor())


